﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bunian.Core;
using Bunian.Core.BusinessObjects;
using System.Text;
using System.Collections.Specialized;
using Shut;

namespace Bunian.Web.Common.Code
{
    public static class BeneficiaryFormattingHelper
    {
        public static string GetProvider(Family subjectFamily)
        {
            string providerText = string.Empty;

            switch (subjectFamily.ProviderKinship)
            {
                case Kinship.Father:
                    providerText = Utils.GetResourceValue("ProviderTitle_Father"); ;
                    break;

                case Kinship.Mother:
                    providerText = Utils.GetResourceValue("ProviderTitle_Mother");
                    break;

                case Kinship.AuntFatherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_AuntFatherSide"); ;
                    break;

                case Kinship.AuntMotherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_AuntMotherSide"); ;
                    break;

                case Kinship.GrandfatherFatherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_GrandfatherFatherSide"); ;
                    break;

                case Kinship.GrandfatherMotherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_GrandfatherMotherSide"); ;
                    break;

                case Kinship.GrandmotherFatherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_GrandmotherFatherSide"); ;
                    break;

                case Kinship.GrandmotherMotherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_GrandmotherMotherSide"); ;
                    break;

                case Kinship.UncleFatherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_UncleFatherSide"); ;
                    break;

                case Kinship.UncleMotherSide:
                    providerText = Utils.GetResourceValue("ProviderTitle_UncleMotherSide"); ;
                    break;

                case Kinship.Other:
                    providerText = Utils.GetResourceValue("ProviderTitle_Other"); ;
                    break;

                default:
                    providerText = Utils.GetResourceValue("ProviderTitle_Other"); ;
                    break;
            }

            return providerText;
        }

        public static string GetCoveredCount(Family subjectFamily)
        {
            if (null != subjectFamily)
            {
                int numberOfChildren = subjectFamily.FamilyMembers.Count;

                int coveredChildren = subjectFamily.GetCoveredCount();

                return coveredChildren + " out of " + numberOfChildren;
            }
            else
            {
                return "No Family";
            }
        }

        public static string GetFamilyInfo(Family subjectFamily)
        {
            if (null != subjectFamily)
            {
                int numberOfChildren = subjectFamily.FamilyMembers.Count;

                if (numberOfChildren == 1)
                {
                    return Utils.GetResourceValue("ChildCount_OneChild");
                }
                else if (numberOfChildren == 2)
                {
                    return Utils.GetResourceValue("ChildCount_TwoChildren");
                }
                else if (numberOfChildren <= 10)
                {
                    return string.Format(Utils.GetResourceValue("ChildCount_MultipleLessThanTen"), numberOfChildren);
                }
                else
                {
                    return string.Format(Utils.GetResourceValue("ChildCount_MultipleMoreThanTen"), numberOfChildren);
                }
            }
            else
            {
                return "No Family";
            }
        }

        public static string GetSex(Child subjectChild)
        {
            string gender = string.Empty;

            switch (subjectChild.ContactInfo.Sex)
            {
                case Gender.Male:
                    gender = "Male";

                    break;

                case Gender.Female:
                    gender = "Female";

                    break;
            }

            return gender;
        }

        internal static string GetChildDescription(Child currentChild)
        {
            StringCollection tenderTitles = new StringCollection();
            string joinedTenderTitles;

            if (null != currentChild.BeneficiaryTenders && currentChild.BeneficiaryTenders.Count > 0)
            {
                foreach (Tender childTender in currentChild.BeneficiaryTenders)
                {
                    string tenderTitle = GetChildTenderTitle(childTender, currentChild.ContactInfo.Sex);

                    tenderTitles.Add(tenderTitle);
                }

                joinedTenderTitles = GetJoinedTenderTitles(tenderTitles);
            }
            else
            {
                joinedTenderTitles = Utils.GetResourceValue("TenderType_ChildMale");
            }

            string finalDescription = joinedTenderTitles + " " + GetAgeDescription(currentChild);

            return finalDescription;
        }

        private static string GetYearCountInWords(int age)
        {
            //TODO: support proper multilingual
            //Now Support arabic only

            string template = "{0} {1}";
            
            string countDescription = string.Empty;

            if (age == 0)
            {
                countDescription = Utils.GetResourceValue("Child_YearInWordsLessThanYear");
            }
            else if (age == 1 || (age > 10))
            {
                countDescription = Utils.GetResourceValue("Child_YearInWordsOne");
            }
            else if (age == 2)
            {
                countDescription = Utils.GetResourceValue("Child_YearInWordsTwo"); ;
            }
            else if (age < 10)
            {
                countDescription = Utils.GetResourceValue("Child_YearInWordsLessThanTen"); ;
            }

            return string.Format(template, age.ToString(), countDescription);
        }

        private static string GetAgeDescription(Child currentChild)
        {
            string format = GetMaleFemaleTitle(Utils.GetResourceValue("Child_AgeDescriptionMale"), Utils.GetResourceValue("Child_AgeDescriptionFemale"), currentChild.ContactInfo.Sex)  + " {0}";
            
            int age = currentChild.GetAgeInYears();

            return string.Format(format, GetYearCountInWords(age));
        }

        private static string GetJoinedTenderTitles(StringCollection tenderTitles)
        {
            //TODO: Add multi-language support
            //If language == arabic
            string[] titles = new string[tenderTitles.Count];
            
            tenderTitles.CopyTo(titles, 0);

            return string.Join(Utils.GetResourceValue("Global_ArabicSeparator"),titles);
        }

        private static string GetMaleFemaleTitle(string maleTitle, string femaleTitle, Gender gender)
        {
            string result;

            switch (gender)
            {
                case Gender.Female:
                    result = femaleTitle;
                    break;

                case Gender.Male:
                    result = maleTitle;
                    break;

                default:
                    result = maleTitle;
                    break;
            }

            return result;
        }

        private static string GetChildTenderTitle(Tender childTender, Gender gender)
        {
            //Can be cached later
            Dictionary<TenderType, string> tenderTypes = new Dictionary<TenderType, string>();

            tenderTypes.Add(TenderType.Orphan, GetMaleFemaleTitle(Utils.GetResourceValue("TenderType_OrphanMale"), Utils.GetResourceValue("TenderType_OrphanFemale"), gender));
            tenderTypes.Add(TenderType.Patient, GetMaleFemaleTitle(Utils.GetResourceValue("TenderType_PatientMale"), Utils.GetResourceValue("TenderType_PatientFemale"), gender));
            tenderTypes.Add(TenderType.Student, GetMaleFemaleTitle(Utils.GetResourceValue("TenderType_StudentMale"), Utils.GetResourceValue("TenderType_StudentFemale"), gender));

            return tenderTypes[childTender.Type];
        }
    }
}
